package com.zwy.rxjava;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

public class RXJavaDemo6 extends Activity {

	/** 
     * 如果在doOnSubscribe()之后指定了subscribeOn()，它决定了doOnSubscribe()在哪种线程中执行。 
     * （1）doOnSubscribe()之前的subscribeOn()不会影响它。 
     * （2）doOnSubscribe()之后的subscribeOn()，且是最近的才会影响它。 
     */  
	//http://blog.csdn.net/u010746364/article/details/51428976
	
	Button btn;
	TextView txt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.rxjavademo6);

		LogUtils.allowD = true;
		LogUtils.customTagPrefix = "itchen";

		btn = (Button) findViewById(R.id.btn);
		txt = (TextView) findViewById(R.id.txt);

		txt.setVisibility(View.GONE);

		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//test();
				test2();
			}
		});
	}

	private String name0 = null;
	private String name1 = null;
	private String name2 = null;
	private String nameInThread = null;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				Toast.makeText(getApplicationContext(), name0, Toast.LENGTH_SHORT).show();
			} else if (msg.what == 1) {
				Toast.makeText(getApplicationContext(), name1, Toast.LENGTH_SHORT).show();
			} else if (msg.what == 2) {
				Toast.makeText(getApplicationContext(), name2, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getApplicationContext(), nameInThread, Toast.LENGTH_SHORT).show();
			}
		}
	};
	
	
	private void test2() {  
	    Observable.create(new Observable.OnSubscribe<Integer>() {  
	        @Override  
	        public void call(Subscriber<? super Integer> subscriber) {  
	        	
	        	LogUtils.getThreadName("call");
	            subscriber.onNext(2323);  
	            name2 = "name2:" + Thread.currentThread().getName();  
	            handler.sendEmptyMessage(2);  
	            subscriber.onCompleted();
	            //RxCachedThreadScheduler-2发送触发线程
	        }  
	    }).subscribeOn(Schedulers.io())  
	            .doOnSubscribe(new Action0() {  
	                @Override  
	                public void call() {  
	                	
	                	LogUtils.getThreadName("doOnSubscribe");
	                    name0 = "name0:" + Thread.currentThread().getName();  
	                    handler.sendEmptyMessage(0);  
	                   
	                }  
	            })  
	            .observeOn(Schedulers.io())  
	            .subscribe(new Action1<Integer>() {  
	                @Override  
	                public void call(Integer integer) {  
	                	LogUtils.getThreadName("subscribe");//消费线程在RxCachedThreadScheduler-1
	                    name1 = "name1:" + Thread.currentThread().getName();  
	                    handler.sendEmptyMessage(1);  
	                }  
	            });  
	    
//	    11-07 10:34:16.290: D/itchen:LogUtils.getThreadName(L:290)(11261): itchen-threadName----doOnSubscribe---->main
//	    11-07 10:34:16.300: D/itchen:LogUtils.getThreadName(L:290)(11261): itchen-threadName----call---->RxCachedThreadScheduler-2
//	    11-07 10:34:16.300: D/itchen:LogUtils.getThreadName(L:290)(11261): itchen-threadName----subscribe---->RxCachedThreadScheduler-1

	}  
	

	/**
	 * 默认情况下subcribe发生的线程决定了，doOnSubscribe()执行所在的线程。
	 * 这里nameInThread和name0的值是相等的。都是在同一个子线程中执行，这里就是new Thread()这个线程。
	 */
	private void test() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				nameInThread = "nameInThread:" + Thread.currentThread().getName();
				handler.sendEmptyMessage(0x99);

				Observable.create(new Observable.OnSubscribe<Integer>() {
					@Override
					public void call(Subscriber<? super Integer> subscriber) {//RxCachedThreadScheduler-2

						LogUtils.getThreadName("call");
						//	11-07 10:19:03.340: D/itchen:LogUtils.getThreadName(L:290)(10625): itchen-threadName----call---->RxCachedThreadScheduler-2

						subscriber.onNext(2323);
						name2 = "name2:" + Thread.currentThread().getName();
						handler.sendEmptyMessage(2);
					}

				}).subscribeOn(Schedulers.io())//RxCachedThreadScheduler-2就是
						.doOnSubscribe(new Action0() {
							@Override
							public void call() {

								LogUtils.getThreadName("doOnSubscribe");
								//它是执行在它所在的默认的线程内.
								//	11-07 10:19:03.330: D/itchen:LogUtils.getThreadName(L:290)(10625): itchen-threadName----doOnSubscribe---->Thread-704

								name0 = "name0:" + Thread.currentThread().getName();
								handler.sendEmptyMessage(0);
							}
						}).observeOn(Schedulers.io())//
						.subscribe(new Action1<Integer>() {//RxCachedThreadScheduler-1
									@Override
									public void call(Integer integer) {

										LogUtils.getThreadName("subscribe");
										//11-07 10:19:03.340: D/itchen:LogUtils.getThreadName(L:290)(10625): itchen-threadName----subscribe---->RxCachedThreadScheduler-1

										name1 = "name1:" + Thread.currentThread().getName();
										handler.sendEmptyMessage(1);
									}
								});
			}
		}).start();
	}

	//	11-07 10:19:03.330: D/itchen:LogUtils.getThreadName(L:290)(10625): itchen-threadName----doOnSubscribe---->Thread-704
	//	11-07 10:19:03.340: D/itchen:LogUtils.getThreadName(L:290)(10625): itchen-threadName----call---->RxCachedThreadScheduler-2
	//	11-07 10:19:03.340: D/itchen:LogUtils.getThreadName(L:290)(10625): itchen-threadName----subscribe---->RxCachedThreadScheduler-1

}
